package com.muqi.app.qlearn.student;

import static nl.qbusict.cupboard.CupboardFactory.cupboard;

import java.io.Serializable;
import java.util.List;

import com.muqi.app.project.contants.MContants;
import com.muqi.app.user.db.ChildBean;
import com.muqi.app.user.db.CupboardSQLiteOpenHelper;
import com.muqi.app.user.widget.ChildrenWindow;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

public abstract class BaseChildFragment extends BaseFragment implements
		ChildrenWindow.ChildrenListener {
	private CupboardSQLiteOpenHelper dbHelper;
	protected SQLiteDatabase sqlDb;
	protected List<ChildBean> myChildren;
	private ChildrenWindow childWindow = null;
	protected ChildBean currentChild = null;
	private ChildChangeReceiver changeReceiver;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbHelper = new CupboardSQLiteOpenHelper(getActivity());
		sqlDb = dbHelper.getWritableDatabase();
		myChildren = cupboard().withDatabase(sqlDb).query(ChildBean.class)
				.list();

		if (myChildren != null && myChildren.size() > 0) {
			if (mSpUtil.getUserType().equals(MContants.LOGIN_AS_PARENTS)) {
				childWindow = new ChildrenWindow(getActivity(), this,
						myChildren);
			}

			for (ChildBean child : myChildren) {
				if (child.child_id.equals(mSpUtil.getCurrentChildId())) {
					currentChild = child;
					break;
				}
			}
		}
		changeReceiver=new ChildChangeReceiver();
		
		getActivity().registerReceiver(changeReceiver, new IntentFilter("childChanged"));
	}

	public void showChildrenWindow(View v) {
		if (childWindow != null) {
			childWindow.showPopupWindow(v, mSpUtil.getCurrentChildId());
		}
	}

	protected abstract void onChoiceChildResult(ChildBean myChild);

	@Override
	public void onSeleectChild(ChildBean child) {
		if (child.child_id.equals(mSpUtil.getCurrentChildId())) {
			return;
		}
		mSpUtil.setCurrentChildId(child.child_id);
		mSpUtil.setChildClassId(child.class_id);
		mSpUtil.setChildGradeId(child.grade_id);
		mSpUtil.setChildSchoolId(child.school_id);
		mSpUtil.setClassName(child.class_name);
		mSpUtil.setSchoolName(child.school_name);
		mSpUtil.setCurrentIm(child.im_user_id);
		mSpUtil.setCurrentAva(child.avatar_02);
		currentChild = child;

		onChoiceChildResult(child);
		Intent intent = new Intent();
		intent.setAction(MContants.CHANGE_CURRENT_CHILD_ACTION);
		intent.putExtra(MContants.CHANGE_CURRENT_CHILD_DATAS,
				(Serializable) child);
		getActivity().sendBroadcast(intent);
	}

	private class ChildChangeReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context arg0, Intent arg1) {
			if (arg1.getAction().equals("childChanged")) {
				myChildren = cupboard().withDatabase(sqlDb)
						.query(ChildBean.class).list();

				if (myChildren != null && myChildren.size() > 0) {
					if (mSpUtil.getUserType()
							.equals(MContants.LOGIN_AS_PARENTS)) {
						childWindow = new ChildrenWindow(getActivity(),
								BaseChildFragment.this, myChildren);
					}

					for (ChildBean child : myChildren) {
						if (child.child_id.equals(mSpUtil.getCurrentChildId())) {
							currentChild = child;
							break;
						}
					}
				}
			}

		}

	}
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		getActivity().unregisterReceiver(changeReceiver);
	}

}
